// pages/newgame/pages/pointranking/pointranking.js
// import req from '../../../../utils/req.js'
// var util = require('../../../../utils/util.js')
import req from '../../../utils/req'
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selectresult: {
      unionid: wx.getStorageSync('openid'),
      page: 1,
      size: 10
    },
    id: '',
    listData: [],
    hasmore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (!wx.getStorageSync('openid')) {
      wx.login({
        success: res => {
          wx.getUserInfo({
            withCredentials: true,
            success: (res_user) => {
              console.log(res_user);
              var object = {
                record_type: 1,
                auth_type: 0,
                scene_value: 0,
                code: res.code,
                encryptedData: encodeURIComponent(res_user.encryptedData),
                iv: encodeURIComponent(res_user.iv),
              }
              var sign = app.sort_ASCII(object)
              wx.request({
                url: app.globalData.new_url + '/user/login/login',
                data: {
                  record_type: 1,
                  auth_type: 0,
                  scene_value: 0,
                  code: res.code,
                  encryptedData: encodeURIComponent(res_user.encryptedData),
                  iv: encodeURIComponent(res_user.iv),
                  sign: sign,
                },
                method: 'POST',
                header: {
                  'content-type': 'application/json'
                },
                success: (res) => {
                  wx.setStorageSync('session', res.data.hash);
                  wx.setStorageSync('openid', res.data.openid);
                  wx.setStorageSync('open_id', res.data.open_id);
                  this.getlist()
                }
              })
            },
          })
        },

      })
    } else {
      this.getlist()
    }
  },

  async getlist() {
    // 获取数据
    req.getsetp(this.data.selectresult).then(res => {
      if (res.own.own.step && this.data.selectresult.page == 1) {
        res.own.own.index = res.own.index
        res.data = [res.own.own].concat(res.data)
      }
      if (this.data.selectresult.page == 1) {
        this.listData = []
      }
      this.setData({
        listData: this.data.listData.concat(res.data),
        hasmore: !(res.data.length <= this.data.selectresult.size)
      })
    })
  },
  onReachBottom() {
    if (this.data.hasmore) {
      let current = this.data.selectresult.page
      this.data.selectresult.page = current + 1
      this.getlist()
    }
  },
  onPullDownRefresh() {
    this.setData({
      selectresult: {
        unionid: wx.getStorageSync('openId'),
        page: 1,
        size: 20
      }
    })
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '加载中' //加载转圈显示
    })
    this.getlist().then(() => {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  onShareAppMessage() {
    return{title:'“健康生活，快乐运动” 气质范邀请你一起走路拿礼品'}
  }
})